/*

Author   : chaos

FileName : fptr_out.cpp

Date     : 16/08/17 14:57:07

*/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

#define FILEPATH "Reader.bin"

void output_file(fstream &);
int main(int argc ,char *argv[])
{
    fstream file;
    int record_size = sizeof(int)  + 8 * sizeof(char);

    file.open(FILEPATH, ios::out| ios::in | ios::binary);

    if(!file)
    {
        cerr << "Can't open file" << endl;

    }
    file.seekg(0,ios::end);
    cout << "record size:" << record_size << " file size : ";
    cout << file.tellg() << endl;

    output_file(file);

    char str[8] = "Herry";

    file.seekp(record_size * 2 + sizeof(int), ios::beg);

    file.write(str, 8 * sizeof(char));

    cout << endl << "After updating ..." << endl;
    output_file(file);
    return 0;
}

void output_file(fstream & file)
{
    char str[8];
    int id;

    file.seekg(0, ios::beg);

    cout << setw(4) << setiosflags(ios::right) << "ID"
        << setw(8) << setiosflags(ios::right) << "Name" << endl;
    cout << "----------------------------------" << endl;

    file.read((char *) &id, sizeof(int));
    file.read((char *) str, 8 * sizeof(char));

    while(!file.eof())
    {
        cout << setw(4) << setiosflags(ios::right) << id
            << setw(8) << setiosflags(ios::right) << str << endl;

        file.read((char *) &id ,sizeof(int));
        file.read((char *) str,8 * sizeof(char));
    }
    file.clear();
}
